﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExamAnalysis.Model;
using ExamAnalysis.Core;
using Ext.Net;
using System.IO;
using System.Xml;
using System.Web.Security;


namespace ExamAnalysis
{
    public partial class QuickReports : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DdExamGoup.DataSource = new ExamAnalysisDataContext().ExamGroup.ToList();
                DdExamGoup.DataBind();
                DdSchoolCalender.DataSource = new ExamAnalysisDataContext().SchoolCalender.ToList();
                DdSchoolCalender.DataBind();
                int scId = int.Parse(DdSchoolCalender.SelectedValue);
                DdExam.DataSource = new ExamAnalysisDataContext().ExamInfo.Where(s => s.CalendarID == scId).ToList();
                DdExam.DataBind();
                BindRoles();
                ViewState.Add("id", DdExamGoup.SelectedValue);
                ViewState.Add("type", "group");
                BindReport();
            }
        }

        protected void BindRoles()
        {

            List<object> list = new List<object>
            {
                new {Text = "请选择", Value = ""}
            };
            list.Remove(list[0]);
            string[] roles = Roles.GetRolesForUser();
            if (User.IsInRole("admin"))
                list.Add(new { Text = "超级管理员", Value = "admin" });
            if (User.IsInRole("school"))
                list.Add(new { Text = "学校领导", Value = "school" });
            if (User.IsInRole("grade"))
                list.Add(new { Text = "年级组长", Value = "grade" });
            if (User.IsInRole("course"))
                list.Add(new { Text = "教研组长", Value = "course" });
            if (User.IsInRole("class"))
                list.Add(new { Text = "班主任", Value = "class" });
            if (User.IsInRole("teacher"))
                list.Add(new { Text = "任课老师", Value = "teacher" });
            if (User.IsInRole("student"))
                list.Add(new { Text = "普通学生", Value = "student" });

            Ddrole.DataSource = list;
            Ddrole.DataBind();
            ViewState.Add("role", Ddrole.SelectedValue);
        }

        protected void BindReport()
        {
            List<ValueTextEntity> MicroItems = new List<ValueTextEntity>();
            List<ValueTextEntity> MacroItems = new List<ValueTextEntity>();
            List<ValueTextEntity> CompareItems = new List<ValueTextEntity>();
            
            string role= Ddrole.SelectedValue;
            //总分
            MacroItems.AddRange(Core.Menu.GetReportList(role, Core.Menu.ReportType.MacroReports));
            //对比
            CompareItems.AddRange(Core.Menu.GetReportList(role, Core.Menu.ReportType.CompareReports));
            //小分
            MicroItems.AddRange(Core.Menu.GetReportList(role, Core.Menu.ReportType.MicroReports));

            //处理空白
            if (MacroItems.Count == 0)
                MacroItems.Add(new ValueTextEntity("暂无", "#"));
            if (MicroItems.Count == 0)
                MicroItems.Add(new ValueTextEntity("暂无", "#"));
            if (CompareItems.Count == 0)
                CompareItems.Add(new ValueTextEntity("暂无", "#"));
            foreach (ValueTextEntity vte in MacroItems)
            { 
                vte.Value=queryStringAdd( vte.Value);
            }
            foreach (ValueTextEntity vte in MicroItems)
            { 
                vte.Value=queryStringAdd( vte.Value);            
            }
            foreach (ValueTextEntity vte in CompareItems)
            {
                vte.Value = queryStringAdd(vte.Value);            
            }

            MacroRepeater.DataSource = MacroItems;
            MacroRepeater.DataBind();
            MicroRepeater.DataSource = MicroItems;
            MicroRepeater.DataBind();
            CompareRepeater.DataSource = CompareItems;
            CompareRepeater.DataBind();
        }



        protected void ExamLink_Click(object sender, EventArgs e)
        {
            ExamLink.Enabled = false;
            ExamLink.Font.Bold = true;
            GroupLink.Enabled = true;
            GroupLink.Font.Bold = false;
            PanelExam.Visible = true;
            PanelExamGroup.Visible = false;
        }

        protected void GroupLink_Click(object sender, EventArgs e)
        {
            ExamLink.Enabled = true;
            ExamLink.Font.Bold = false;
            GroupLink.Enabled = false;
            GroupLink.Font.Bold = true;
            PanelExam.Visible = false;
            PanelExamGroup.Visible = true;
        }

        protected void BindExam()
        {
            int scId = int.Parse(DdSchoolCalender.SelectedValue);
            //DdExam.DataSource = new ExamAnalysisDataContext().ExamInfo.Where(s => s.CalendarID == scId).ToList();
            DdExam.DataSource = RoleData.GetExamInfoOfRole(Membership.GetUser().UserName, Ddrole.SelectedValue)
                .Where(s => s.CalendarID == scId).ToList();
            DdExam.DataBind();
            ViewState.Add("id", DdExam.SelectedValue);
            ViewState.Add("type", "exam");
            BindReport();
        }

        protected void DdSchoolCalender_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindExam();
        }

        protected void Ddrole_SelectedIndexChanged(object sender, EventArgs e)
        {
            ViewState.Add("role", Ddrole.SelectedValue);
            BindExam();
            BindReport();
        }

        protected void DdExamGoup_SelectedIndexChanged(object sender, EventArgs e)
        {
            ViewState.Add("id", DdExamGoup.SelectedValue);
            ViewState.Add("type", "group");
            BindReport();
        }

        protected void DdExam_SelectedIndexChanged(object sender, EventArgs e)
        {
            ViewState.Add("id", DdExam.SelectedValue);
            ViewState.Add("type", "exam");
            BindReport();
        }

        protected string queryStringAdd(string url)
        {
            if (ViewState["type"] == null || ViewState["id"] == null || ViewState["role"] == null)
                return "";
            else
                return url + "&type=" + ViewState["type"].ToString() + "&id=" + ViewState["id"].ToString();

        }

    }
}